{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89430a3a",
   "metadata": {},
   "source": [
    "# K最近邻算法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70ba4b03",
   "metadata": {},
   "source": [
    "## K最近邻算法的原理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a56ab32",
   "metadata": {},
   "source": [
    "- K最近邻算法就是目标数据点在现有数据集中，离哪一部分最近就将其划分为哪一部分。而K字母的含义就是近邻的个数\n",
    "- 在scikit-learn中，K最近邻算法的K值是通过N_neighbors参数来调节的，默认值是5.\n",
    "-  K最近邻算法在回归和分类都可以运用，原理是相同的。当我们使用K最近邻回归计算某个数据点的预测值时，模型会选择离该数据点最近的若干个数据集中的点，并将它们的y值取平均值，并把改平均值作为新数据点的预测值。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c75f7aef",
   "metadata": {},
   "source": [
    "\n",
    "### K最近邻算法，在python中的实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "013fe874",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_wine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "484b487c",
   "metadata": {},
   "outputs": [],
   "source": [
    "wine_dataset = load_wine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c82fd808",
   "metadata": {},
   "outputs": [],
   "source": [
    "wine_dataset.keys() # 了解数据集中有那些属性\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7f57de4",
   "metadata": {},
   "outputs": [],
   "source": [
    "wine_dataset['data'].shape # 查看数据矩阵信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18f03235",
   "metadata": {},
   "outputs": [],
   "source": [
    "wine_dataset['DESCR'] # 查看数据集的基本描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa24e603",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入数据集拆分工具,把他们分成训练集与测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(\n",
    "wine_dataset['data'],wine_dataset['target'],random_state=0\n",
    ") # random_state生成伪随机数以他为9依据拆分。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "662086b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(wine_dataset['data'].shape,X_train.shape,X_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "026b6e18",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入KNN分类模型\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "# 指定k值为1\n",
    "knn = KNeighborsClassifier(n_neighbors=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfad4ee0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用训练集拟合模型\n",
    "knn.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e789468",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(knn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d768232a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对模型的预测进行打分\n",
    "print(knn.score(X_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8087481",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假如我们获得一个新的数据\n",
    "X_new = np.array(\n",
    "    [[13.2,2.77,2.51,18.5,96.6,1.04,2.55,0.57,1.47,6.2,1.05,3.33,820]]\n",
    ")\n",
    "result = knn.predict(X_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62c37e51",
   "metadata": {},
   "outputs": [],
   "source": [
    "wine_dataset['target_names'][int(result)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d348eaaa",
   "metadata": {},
   "source": [
    "# 朴素贝叶斯\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "187fdfb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: array([2, 1, 0, 4]), 1: array([1, 3, 3, 0])}\n"
     ]
    }
   ],
   "source": [
    "# 导入\n",
    "import numpy as np\n",
    "X = np.array([[0,1,0,1],\n",
    "              [1,1,1,0],\n",
    "              [0,1,1,0],\n",
    "              [0,0,0,1],\n",
    "              [0,1,1,0],\n",
    "              [1,0,0,1],\n",
    "              [1,0,0,1],\n",
    "             ])\n",
    "y = np.array([0,1,1,0,1,0,0])\n",
    "\n",
    "counts = {}\n",
    "\n",
    "for label in np.unique(y):\n",
    "    counts[label] = X[y == label].sum(axis=0)\n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "afbecbf8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 0]\n",
      " [0 1 1 0]\n",
      " [0 1 1 0]]\n"
     ]
    }
   ],
   "source": [
    "print(X[y==label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5e154883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xiayu\n",
      "[[0.13848881 0.86151119]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.naive_bayes import BernoulliNB\n",
    "clf = BernoulliNB()\n",
    "clf.fit(X,y)\n",
    "Next_Day = [[0,0,1,0]]\n",
    "pre = clf.predict(Next_Day)\n",
    "\n",
    "if pre == [1]:\n",
    "    print('xiayu')\n",
    "else:\n",
    "    print('not xia yu ')\n",
    "print(clf.predict_proba(Next_Day))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d754c3b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "not xia yu \n",
      "[[0.92340878 0.07659122]]\n"
     ]
    }
   ],
   "source": [
    "Another_day = [[1,1,0,1]]\n",
    "pre2 = clf.predict(Another_day)\n",
    "\n",
    "if pre2 == [1]:\n",
    "    print('xiayu')\n",
    "else:\n",
    "    print('not xia yu ')\n",
    "print(clf.predict_proba(Another_day))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af24a4d8",
   "metadata": {},
   "source": [
    "## 朴素贝叶斯分多种\n",
    "- 贝努利朴素贝叶斯（0－1分布）\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "nb = BernoulliNB()\n",
    "- 高斯朴素贝叶斯（正态分布）\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "gnb = GaussianNB()\n",
    "- 多项式朴素贝叶斯（朴素贝叶斯）\n",
    "from sklearn.naive_bayes import MultionmialNB \n",
    "mnb = MultinomialNB()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e29d0a72",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
